import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_syt/application.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/utils/click_utils.dart';

class ServideMessagesInput extends StatefulWidget {
  final TextEditingController controller;
  final Function clickPhoto;
  final Function(String msg) sendMsgBlock;
  ServideMessagesInput(
      {required this.controller,
      required this.sendMsgBlock,
      required this.clickPhoto});
  @override
  ServideMessagesInputState createState() => ServideMessagesInputState();
}

class ServideMessagesInputState extends State<ServideMessagesInput> {
  bool isShowInput = false;
  double bottom = ScreenUtil().bottomBarHeight;
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Container(
          color: Color(0xffE4e4e4),
          height: 1,
        ),
        Container(
          // margin: EdgeInsets.only(
          //     bottom: ),
          // height: 50.w,
          color: Color(0xfff7f7f7),
          padding: EdgeInsets.only(
              left: 15.w,
              right: 15.w,
              top: 7.w,
              bottom: 7.w + (isShowInput ? 0 : bottom)),
          constraints: BoxConstraints(
            //   maxHeight: 200.w,
            minHeight: 50.w,
          ),
          child: Row(
            // crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // Icon(Icons.image),
              GestureDetector(
                onTap: ClickUtils.debounce(() {
                  widget.clickPhoto();
                }),
                behavior: HitTestBehavior.opaque,
                child: Image.asset(
                  'assets/icons/icon_service_photo.png',
                  width: 28,
                  height: 28,
                ),
              ),
              Expanded(
                  child: Padding(
                padding: EdgeInsets.symmetric(
                  horizontal: 8,
                ),
                child: TextField(
                  enableInteractiveSelection: true,
                  maxLines: null,
                  maxLength: 200,
                  inputFormatters: [
                    FilteringTextInputFormatter.deny(appContext.emojiRegexp)
                  ],
                  controller: widget.controller,
                  cursorColor: ThemeColor.primaryColor,
                  textInputAction: TextInputAction.send,

                  onSubmitted: (value) {
                    if (value.isNotEmpty) {
                      //  if (widget.controller.text.isNotEmpty) {
                      widget.sendMsgBlock(widget.controller.text);
                      // }
                    }
                  },
                  decoration: InputDecoration(
                      counterText: '',
                      contentPadding: EdgeInsets.all(8),
                      border: InputBorder.none,
                      filled: true,
                      isDense: true,
                      hintText: S.current.service_input,
                      enabledBorder: OutlineInputBorder(
                        /*边角*/
                        borderRadius: BorderRadius.all(
                          Radius.circular(5), //边角为5
                        ),
                        borderSide: BorderSide(
                          color: Colors.white, //边线颜色为白色
                          width: 0, //边线宽度为2
                        ),
                      ),
                      focusedBorder: OutlineInputBorder(
                        /*边角*/
                        borderRadius: BorderRadius.all(
                          Radius.circular(5), //边角为5
                        ),
                        borderSide: BorderSide(
                          color: Colors.white, //边线颜色为白色
                          width: 0, //边线宽度为2
                        ),
                      ),
                      hintStyle: TextStyle(
                          color: Colors.black.withOpacity(0.3), fontSize: 13),
                      fillColor: ThemeColor.bkColorFFFFFF),
                ),
              )),
              GestureDetector(
                onTap: ClickUtils.debounce(() {
                  if (widget.controller.text.isNotEmpty) {
                    widget.sendMsgBlock(widget.controller.text);
                  }
                }),
                child: Container(
                  width: 77,
                  height: 35,
                  decoration: BoxDecoration(
                      color: ThemeColor.primaryColor,
                      borderRadius: BorderRadius.circular(8)),
                  alignment: Alignment.center,
                  child: Text(
                    S.current.service_send,
                    style: TextStyle(
                        color: Colors.black,
                        fontWeight: FontWeight.w500,
                        fontSize: 14),
                  ),
                ),
              )
            ],
          ),
        )
      ],
    );
  }

  @override
  void initState() {
    super.initState();
    KeyboardVisibilityController().onChange.listen((bool visible) {
      if (visible != isShowInput) {
        if (mounted) {
          setState(() {
            isShowInput = visible;
          });
        }
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void didUpdateWidget(ServideMessagesInput oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }
}
